import sys
sys.path.insert(0, "/home/lighthouse/my-dream")
from module import config
from module.dir import words_ch, words, words_list

def translate_main():
    from entrance import main_entran
    input_num = input("欢迎来到词语查询系统\n请输入您想要进1行的操作\n"
                      "1. 英译汉\n2. 汉译英\n3. 单词背诵\n4.返回主菜单")
    if input_num == "1":
        from middle_script import save_file
        input_en = input("请输入您要查询的单词: ")
        from trans_api import connect
        # 调用有道API查询该词
        results = connect(input_en=input_en)
        char_en = input_en
        # 网络释义
        explain_chs = results["basic"]['explains']
        print("单词: " + char_en)
        char_dict = {}
        value_list = []
        #　调用保存文件的方法
        save_file().save_txt(filename=words, text="单词: " + char_en + "\n")
        for explain_ch in explain_chs:
            print("         " + explain_ch)
            save_file().save_txt(filename=words, text="         " + explain_ch + "\n")
            value_list.append(explain_ch)
        # 保存为键值对的value
        char_dict[char_en] = value_list
        #print(char_dict)
        save_file().save_txt(filename=words_list, text=str(char_dict) + "\n")
        wait = input("返回主菜单/退出程序(y/n)")
        if wait == "n":
            sys.exit(0)
        elif wait == "y":
            main_entran()
    if input_num == "2":
        from middle_script import save_file
        input_en = input("请输入您要查询的汉字: ")
        from trans_api import connect
        # 调用有道API查询该词
        results = connect(input_en=input_en)
        char_en = input_en
        # 网络释义
        explain_chs = results["web"][0]['value']
        print("汉字: " + char_en)
        # 　调用保存文件的方法
        save_file().save_txt(filename=words_ch, text="汉字: " + char_en + "\n")
        for explain_ch in explain_chs:
            print("         " + explain_ch)
            save_file().save_txt(filename=words_ch,
                                 text="         " + explain_ch + "\n")
        wait = input("返回主菜单/退出程序(y/n)")
        if wait == "n":
            sys.exit(0)
        elif wait == "y":
            main_entran()
    if input_num == "3":
        choose_language = input("系统将会随机进行挑选单词\n"
                                "请选择你的出题语言   \n"
                                "1.英-->中\n"
                                "2.中-->英")
        if choose_language == '1':
            from src.middle_script import trans
            trans().en_to_ch()
        wait = input("返回主菜单/退出程序(y/n)")
        if wait == "n":
            sys.exit(0)
        elif wait == "y":
            main_entran()
    if input_num == "4":
            main_entran()
